package adri.exam.struts;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


public class ProveedorAction extends ActionSupport implements ModelDriven<Proveedor>{

private static final long serialVersionUID = -6659354552584240539L;

	
	private Proveedor proveedor = new Proveedor();
	private List<Proveedor> listaProveedores = new ArrayList<Proveedor>();
	private ProveedorDAO proveedorDAO = new ProveedorDAO();

	
	@Override
	public Proveedor getModel() {
		return proveedor;
	}
	
	/**
	 * To save or update user.
	 * @return String
	 */
	public String guardarOActualizar()
	{	
		proveedorDAO.GuardarOActualizarProveedor(proveedor);
		return SUCCESS;
	}
	
	/**
	 * To list all users.
	 * @return String
	 */
	public String listar()
	{
		listaProveedores = proveedorDAO.listarProveedores();
		return SUCCESS;
	}
	
	/**
	 * To delete a user.
	 * @return String
	 */
	public String eliminar()
	{
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		proveedorDAO.eliminarProveedor(Long.parseLong(request.getParameter("id")));
		return SUCCESS;
	}
	
	/**
	 * To list a single user by Id.
	 * @return String
	 */
	public String editar()
	{
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		proveedor = proveedorDAO.listarProveedorPorId(Long.parseLong(request.getParameter("id")));
		return SUCCESS;
	}
	
	public Proveedor getProveedor() {
		return proveedor;
	}

	public void setProveedor(Proveedor proveedor) {
		this.proveedor = proveedor;
	}

	public List<Proveedor> getListaProveedores() {
		return listaProveedores;
	}

	public void setListaProveedores(List<Proveedor> listaProveedores) {
		this.listaProveedores = listaProveedores;
	}
	
}
